<?php
/**
 * Created by JetBrains PhpStorm.
 * User: cgp
 * Date: 13-10-23
 * Time: 下午4:44
 */
//session_start();
include('../common/autoload.php');
use pft\Member\MemberAccount;

//$_SESSION['a'] = 'b';

//session_destroy();
$action = trim($_REQUEST['action']);
$token = $_REQUEST['token'];
//if($token != $_SESSION['csrf_token']) {
//    exit('Unknow Request');
//}
switch($action) {
    //忘记密码，分手机验证和
    case 'ChkPassport':
        $passport = trim($_REQUEST['mobile']);
        $passport = safetxt($passport);
        $chk_res = MemberAccount::chkPassport($passport, $GLOBALS['le']);
        if($chk_res){
            echo '{"status":"ok","msg":"存在该账号","mobile":"'.$chk_res.'"}';
        }
        else {
            echo '{"status":"fail","msg":"不存在该账号"}';
        }
        break;
    case 'SendVcode':
        $mobile = safetxt($_REQUEST['mobile']);
        $stype = safetxt($_REQUEST['stype']);
        $sms_tpl_file = TPLPATH."sms/{$stype}.txt";
       // echo $sms_tpl_file;
        //TODO:账号验证，正式上线后开启
        /*
        if($stype=='register') {
            if(MemberAccount::chkPassport($mobile ,$GLOBALS['le'])){
                exit('{"status":"fail","msg":"该手机号用户已注册票付通会员，请尝试更换其它号码，若有疑问请联系我们！"}');
            }
        }
        */
        if(file_exists($sms_tpl_file)) {
            $tpl = file_get_contents($sms_tpl_file);
        }
        else {
            exit ('{"status":"fail","msg":"短信模板不存在！"}');
        }
        $soap = SoapInit::GetSoapInside();
        $r = MemberAccount::sendVcode($mobile, $tpl, $soap);
        if($r==100) {
            echo '{"status":"ok","msg":"发送验证码成功！","passport":"'.$mobile.'"}';
        }
        else {
            echo '{"status":"fail","msg":"发送失败","code":'.$r.'}';
        }
        break;
    case 'VerifyVcode':
        $mobile = $_REQUEST['mobile'];
        //print_r($_SESSION);
        $vcode = strtolower($_REQUEST['vcode']);
        $now = $_SERVER['REQUEST_TIME'];
        if($now-$_SESSION['timer'] > 43200) {
            unset($_SESSION['vcode']);
        }
        if(isset($_SESSION['vcode'])
            && $vcode==$_SESSION['vcode']) {
            echo '{"status":"ok","msg":"验证通过！"}';
        }
        else{
            echo '{"status":"fail","msg":"验证失败"}';
        }
        break;
    case 'MemberRegister':
        $pData1 = array();
        if(trim($_POST['vcode']) != $_SESSION['vcode'] ) {
            exit('{"status":"fail","msg":"验证码错误！"}');
        }
        $pData1['dname'] = safetxt($_POST['username']);
        $pData1['dtype'] = intval($_POST['dtype']);
        $pData1['mobile'] = safetxt($_POST['mobile']);
        if(!empty($_POST['qq'])) {
            $pData1['qq'] = is_numeric($_POST['qq']) ? trim($_POST['qq']) :
                exit('{"status":"fail","msg":"QQ号码格式错误！"}');
        }
        $pData1['qq'] = trim($_POST['QQ']);
        $pData1['ctel'] = trim($_POST['g_tel']);//固定电话
        $pData1['address'] = safetxt($_POST['com_addr']);
        if($_POST['password']!=$_POST['confirmPwd']) {
            exit('{"status":"fail","msg":"两次密码输入不正确，请重新输入密码！"}');
        }
        $pData1['password'] = md5($_POST['password']);
        //$pData1['cname'] = md5(md5($_POST['password']));

        $pData1['position'] = safetxt($_POST['position']);//职位
        $pData1['email'] = safetxt($_POST['email']);
        $pData1['sex'] = trim($_POST['sex']);//性别
        $pData1['province'] = trim($_POST['d_province']);//
        $pData1['city'] = trim($_POST['d_city']);//
        $pData1['country'] = trim($_POST['d_zone']);//
        $pData1['fax'] = safetxt($_POST['fax']);//传真
        $pData1['com_name'] = safetxt($_POST['company']);//公司
        $pData1['com_type'] = !empty($_POST['company_type']) ? implode(',',$_POST['company_type']) :'';//公司类型
        $pData1['website'] = safetxt($_POST['internet']);//网址

//        print_r($pData1);

        $mem = new MemberAccount($GLOBALS['le']);
        $dist = new Dist;
        echo $mem->register($pData1, $dist);
        break;
    case 'ResetPassword':
        $passport = safetxt($_REQUEST['passport']);
        $p1 = $_REQUEST['pass1'];
        $p2 = $_REQUEST['pass2'];
        $cp = chkPassword($p1, $p2);
        //echo $cp;
        if($cp!=1) {
            $msg = '{"status":"fail","msg":"'.$cp.'"}';
        }
        else {
           $mem = new MemberAccount($GLOBALS['le']);
           $rs = $mem->resetPassword($passport,$p1, $GLOBALS['le']);
           $msg = $rs ? '{"status":"ok","msg":"密码重置成功"}'
                    :'{"status":"fail","msg":"'.$cp.'"}';
        }
        echo $msg;
        break;
    case 'GetMemList':
        $mem = new MemberAccount($GLOBALS['le']);
        $ml = $mem->getMemList($_GET['mobile']);
        print_r($ml);
        break;
    case 'SearchDistor':
        $a = array(
            'status'=>'ok',
            'msg'=>'ookk',
            'body'=>array(100=>'abc',101=>'先行网')
        );
        echo json_encode($a);
        break;
    default:break;
}